package com.kaifamiao.controller;

import com.kaifamiao.domain.User;
import com.kaifamiao.exception.ServiceException;
import com.kaifamiao.service.UserService;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.Map;

@RestController
@RequestMapping("/user")
public class UserController {

    private Logger logger = LoggerFactory.getLogger(this.getClass());

    @Autowired
    private UserService userService;

    @PostMapping("/add")
    public Map<String, Object> add(@RequestBody User user) {
        logger.info( "添加用户" );
        try {
            if(userService.save(user)) {
                return Map.of("success", true, "message", "添加成功" );
            }
            return Map.of("success", false, "message", "添加失败" );
        } catch (ServiceException se) {
            se.printStackTrace();
            return Map.of("success", false, "message", "添加失败", "reason", se.getMessage() );
        }
    }

}
